package com.codedancer.campus.client.api.sms;

import cn.hutool.core.bean.BeanUtil;
import com.codedancer.campus.client.api.sms.ro.SendSmsRo;
import com.codedancer.campus.client.service.ISmsService;
import com.codedancer.campus.client.service.bo.sms.SendSmsBo;
import com.codedancer.campus.common.anno.Limit;
import com.codedancer.campus.common.anno.PassToken;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.github.qcloudsms.httpclient.HTTPException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.IOException;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/sms")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "短信Api", description = "短信Api", tags = "短信Api")
public class SmsApi extends BaseApi {

    @Autowired
    private ISmsService smsService;

    @PostMapping("/send")
    @ApiOperation("发送短信")
    @PassToken
    @Limit(key = "SMS_PHONE", permitsPerSecond = 5, timeout = 1000, msg = "系统繁忙，请稍后再试！")
    public ApiResponse<Void> sendSms(
        @RequestBody @Valid @ApiParam(value = "短信发送Ro", required = true)
            SendSmsRo sendSmsRo) throws HTTPException, IOException {
        SendSmsBo sendSmsBo = BeanUtil.copyProperties(sendSmsRo, SendSmsBo.class);
        smsService.sendSms(sendSmsBo);
        return success();
    }
}
